package com.ecommerce.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回格式
 *
 * @author wang
 */
@Data
public class ResultBody<T> implements Serializable {

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    private ResultBody() {
    }

    public static ResultBody ok() {
        ResultBody r = new ResultBody();
        r.setSuccess(true);
        r.setCode(ReturnCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    public static ResultBody error() {
        ResultBody r = new ResultBody();
        r.setSuccess(false);
        r.setCode(ReturnCode.ERROR);
        r.setMessage("失败");
        return r;
    }

    public ResultBody success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public ResultBody message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResultBody code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResultBody data(T data) {
        this.setData(data);
        return this;
    }
}
